<?php

namespace Biz\model\activity;

use Doraemon\tools\Videos;
use Hhz\Mysql\BaseDbModel;
use Hhzbeads\Img\ImgUrl;
use Hhzbeads\Ini\IniConfig;


class Kohler extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_brand_activity';

    const _DATABASE_ = 'brand_activity';

    const _TABLE_NAME = 'kohler_video';

    public static function getDayList($day)
    {
        $list = self::select('*', ['show_day' => $day, 'ORDER' => ['sort' => 'ASC', 'id' => 'ASC']]);
        $list = self::formatList($list);
        return $list;
    }

    public static function getList()
    {
        $list = self::select('*', ['ORDER' => ['sort' => 'ASC', 'id' => 'ASC']]);
        $list = self::formatList($list);
        return $list;
    }

    public static function getInfo($id)
    {
        $info = self::get('*', ['id' => $id]);
        $list = self::formatList([$info]);
        $info = $list[0] ?? [];
        return $info;
    }

    public static function formatList($list)
    {
        if ($list) {
            $videoIds = array_column($list, 'video_id');
            $videoRes = \Doraemon\model\Videos::select('*', ['id' => $videoIds]);
            $videoInfos = [];
            foreach ($videoRes as $row) {
                $coverPicUrl = \Doraemon\tools\ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $row['cover_pic_id'], ImgUrl::IMAGE_SIZE_ORI_NPHONE);
                $playDomain = Videos::getPlayDomain($row["area"]);
                $row['cover_pic_url'] = $coverPicUrl;
                $row['play_url'] = $playDomain . $row['ori_play_url'];
                $videoInfos[$row['id']] = $row;
            }
            foreach ($list as &$row) {
                $videoCoverImg = $row['video_cover_url'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $row['video_cover_url'], ImgUrl::IMAGE_SIZE_OP_ORI) : '';
                if (empty($videoCoverImg)) {
                    $videoCoverImg = $videoInfos[$row['video_id']]['cover_pic_url'] ?? '';
                }
                $row['tab_img_url'] = $row['tab_img'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $row['tab_img'], ImgUrl::IMAGE_SIZE_OP_ORI) : '';
                $row['poster_url'] = $videoCoverImg;
                $row['video_play_url'] = $videoInfos[$row['video_id']]['play_url'] ?? '';
            }
        }
        return $list;
    }

}